package nix.hibernate;

import nix.utils.HibernateUtils;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

public class DemoObjStatus {

	@Test
	public void fun1() {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		User u = new User();// 瞬时状态
		u.setName("张三");
		session.save(u);// 持久化状态：save方法

		tx.commit();
		session.close();// 游离/托管状态
	}
	
	@Test
	public void fun2() {
		Session session = HibernateUtils.getSession();
		Transaction tx = session.beginTransaction();

		User u = (User) session.get(User.class, 1);
		u.setName("李四");// 不用调用update方法，已经更新了数据：持久化对象的任何操作会自动同步到数据库

		tx.commit();
		session.close();
	}
}
